﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Thanhlichtek.BOS.Sync;

namespace Thanhlichtek.BOS.Win
{
    public partial class OngageAccountForm : Form
    {
        public OngageAccountForm()
        {
            InitializeComponent();
        }

        private String _profileFolder = "Profile";
        private String _fileName = "Profile\\OngageAccount.xml";
        private Ongage.Account _account = new Ongage.Account();
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _account.UserName = txtUserName.Text;
            _account.Password = txtPassword.Text;
            _account.AccountCode = txtAccountCode.Text;

            var newAccount = new Ongage.Account();
            newAccount.UserName = _account.UserName;
            newAccount.Password = CryptoHelper.Encrypt(_account.Password);
            newAccount.AccountCode = _account.AccountCode;

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Ongage.Account));
            if (!Directory.Exists(_profileFolder))
            {
                Directory.CreateDirectory(_profileFolder);
                
            }
            var xw = XmlWriter.Create(_fileName);
            x.Serialize(xw, newAccount);
            xw.Close();
            this.Close();
        }

        private void OngageAccountForm_Load(object sender, EventArgs e)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Ongage.Account));
            if (File.Exists(_fileName))
            {
                XmlReader xr = XmlReader.Create(_fileName);
                _account = (Ongage.Account)x.Deserialize(xr);
                _account.Password = CryptoHelper.Decrypt(_account.Password);

                xr.Close();

                txtUserName.Text = _account.UserName;
                txtPassword.Text = _account.Password;
                txtAccountCode.Text = _account.AccountCode;
                
            }

        }
    }
}
